在 C++ 中,从抽象值到执行的旅程始于 语句。一个 表达式语句 只需在表达式后添加分号即可创建,迫使编译器对其进行求值,并按顺序推进 控制流 的执行。
1. 空语句
一个 空语句 (;) 是一种占位符,当语言要求有语句但你的逻辑不需要时使用。虽然在某些循环中很有用,但要警惕 多余的空语句——在 while 或 if 语句头后误加分号,可能导致毁灭性的逻辑错误,导致预期的代码块被忽略。
⚠️ 警告(第 235 页): 循环头部后意外添加分号会将空语句作为循环体,通常导致无限循环。
2. 复合语句(块)
一个 复合语句,或 块,是由花括号包围的一系列语句。它被视为一个单一的执行单元。块具有自己的作用域;在内部定义的名字在外部不可见。 { }。它被视为一个单一的执行单元。块具有自己的作用域;在内部定义的名字在外部不可见。
注意(第 235 页): 与简单语句不同,块 不 不需要以分号结尾。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates an 'expression statement'?
An expression followed by a semicolon.
Any line of code within curly braces.
A variable declaration without an assignment.
A comment followed by a semicolon.
✅ Correct!
Correct. In C++, adding a semicolon to an expression turns it into a statement.❌ Incorrect
An expression statement specifically refers to an expression terminated by a semicolon.QUESTION 2
Exercise 5.2: What is a block and when might you use one?
A block is a group of functions; used to organize large projects.
A block is a single semicolon; used to end a program.
A block is a sequence of statements in braces; used where the syntax requires one statement but logic requires many.
A block is a reserved memory segment; used for global variables.
✅ Correct!
Precisely. A block (compound statement) allows multiple statements to be treated as one, which is essential for control structures like 'if' and 'while'.❌ Incorrect
Recall that a block is defined by { } and acts as a single execution unit.QUESTION 3
What is the primary danger of an 'extraneous null statement' after a while loop header?
The compiler will throw a syntax error.
The loop body will be treated as the null statement, causing the intended logic to execute only once or never.
It automatically converts the loop into a do-while loop.
It speeds up the execution by skipping checks.
✅ Correct!
Correct. If you write 'while(cond); { body }', the semicolon IS the body, and the block in braces is just a separate statement executed after the loop finishes.❌ Incorrect
The compiler sees it as valid syntax, but it changes the logic entirely—the semicolon becomes the loop's body.QUESTION 4
Exercise 5.3: Does using the comma operator to compress a while loop into a single statement without a block improve readability?
Yes, less lines always means clearer code.
No, it typically diminishes readability by hiding the loop's intent and side effects.
Yes, it is the standard way to write C++ loops.
No, the comma operator cannot be used in while loops.
✅ Correct!
Correct. While syntactically valid, squeezing logic into the loop header using commas makes the code harder to debug and understand.❌ Incorrect
Readable code clearly separates condition checking from the actions taken. Comma operators can obscure this.QUESTION 5
Which statement about block termination is correct?
Every block must end with a semicolon after the closing brace.
A block is NOT terminated by a semicolon.
Only blocks inside functions require a semicolon.
A block must end with a null statement.
✅ Correct!
Correct. The closing brace itself signals the end of the compound statement.❌ Incorrect
Adding a semicolon after a closing brace creates an unnecessary null statement.Statement Logic & Factorials
Applying anatomy of statements to algorithmic logic.
You are tasked with implementing a factorial function and a vowel counter. You must ensure proper scoping and avoid syntax-induced logic errors.
Q
1. [Writing Task] Write your own version of the factorial function ('fact') using a while loop. (Difficulty: Medium, Length: 75 words)
Solution:
A robust version of factorial should use a 'long long' to prevent overflow:
A robust version of factorial should use a 'long long' to prevent overflow:
long long fact(int val) {
long long res = 1;
while (val > 1)
res *= val--; // Expression statement
return res;
}
This function uses a single expression statement as the loop body. A block isn't strictly required here, but could be added for clarity. We decrement 'val' within the expression to move toward the termination condition.Q
2. [Writing Task] Exercise 5.10: Implement a logic block that counts both lower- and uppercase vowels in a string using a switch statement.
Solution:
To count both cases, we stack case labels to share a single block of code:
To count both cases, we stack case labels to share a single block of code:
switch (ch) {
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
// ... repeat for i, o, u
}
By omitting 'break' between 'a' and 'A', the flow of control falls through, effectively counting both characters toward the same counter.